/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import java.util.Iterator;
import javax.swing.JOptionPane;
import stamboom.Administratie;
import stamboom.DatabeesKneksie;
import stamboom.Gezin;
import stamboom.Persoon;

/**
 *
 * @author Cabez
 */
public class PersoonAanmeldDialog extends javax.swing.JDialog {

    /**
     * Creates new form PersoonAanmeldDialog
     */
    private Administratie admin;

    public PersoonAanmeldDialog(java.awt.Frame parent, boolean modal, Administratie adm) {
        super(parent, modal);
        initComponents();
        this.admin = adm;
        this.setModal(modal);
        jcbOuder.removeAllItems();

        jcbOuder.addItem("Onbekend");
        Iterator it = adm.gezinnen();
        if (it != null){
            while (it.hasNext()){
                jcbOuder.addItem(it.next().toString());
            }
        }
        this.persoonPanel2.setPersoonsnummer(this.admin.getNextPersNr());
        /***for (Iterator<Persoon> it = adm.getAllepersonen(); it.hasNext();) {
            jcbOuder.addItem(it.next().toString());
        }

        persoonPanel2.setPersoonsnummer(admin.getNextPersNr());*/
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        persoonPanel2 = new gui.PersoonPanel();
        jcbOuder = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        btnVoegToe = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jcbOuder.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jcbOuderActionPerformed(evt);
            }
        });

        jLabel1.setText("Ouder");

        btnVoegToe.setText("Voeg toe");
        btnVoegToe.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnVoegToeActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(persoonPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(layout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnVoegToe)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(112, 112, 112)
                        .addComponent(jcbOuder, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(persoonPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jcbOuder, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 67, Short.MAX_VALUE)
                .addComponent(btnVoegToe)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jcbOuderActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jcbOuderActionPerformed
       /**String naam = jcbOuder.getSelectedItem().toString();
        Persoon p;

        if (!naam.equals("Onbekend")) {
            p = admin.getPersoonDoorNaam(naam);
            Iterator<Gezin> irG = p.getGezinnen();
            while (irG.hasNext()) {
                Gezin g = irG.next();
            }
        }*/
    }//GEN-LAST:event_jcbOuderActionPerformed

    private void btnVoegToeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnVoegToeActionPerformed
       if(persoonPanel2.getAchternaam() != null && persoonPanel2.getVoornamen() != null){
            if(persoonPanel2.getGeslacht() != null && persoonPanel2.getGebDat() != null){
                if(jcbOuder.getSelectedItem() != null){
                    Gezin selectedGezin = null;
                    int selectedIndexGezin = jcbOuder.getSelectedIndex();
                    if(selectedIndexGezin >= 0)
                    {
                        int t = 0;
                        
                        Iterator<Gezin> gezinnen = admin.gezinnen();
                        while (gezinnen.hasNext()){
                
                        if(t != selectedIndexGezin)
                        {
                            selectedGezin = gezinnen.next();
                            break;
                        }
                        else
                        {
                            gezinnen.next();
                        }
                
                        t++;
                        }
                   } 
                    admin.addPersoon(persoonPanel2.getGeslacht(),persoonPanel2.getVoornamen(),persoonPanel2.getAchternaam(),persoonPanel2.getTussenvoegsel(),persoonPanel2.getGebDat(),selectedGezin);
                    int i = admin.getNextPersNr() -1;
                    Persoon p = admin.getPersoon(i);
                    //DatabeesKneksie dbhandler = new DatabeesKneksie();
                    //dbhandler.//.insertPersoon(p);
                    StamboomFrame sbf = new StamboomFrame(admin);
                    sbf.setVisible(true);
                    this.dispose();
                }else{
                    JOptionPane.showMessageDialog(null,"Geen gezin geselecteerd..", "Error", JOptionPane.ERROR_MESSAGE);
                }
            }else{
            JOptionPane.showMessageDialog(null,"Geslacht of geboorte datum niet ingevult.", "Error", JOptionPane.ERROR_MESSAGE);
        }
        }else{
            JOptionPane.showMessageDialog(null,"Voor- of achternaam niet ingevult.", "Error", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_btnVoegToeActionPerformed
    /**
     * @param args the command line arguments
     */
//    public static void main(String args[]) {
//        /*
//         * Set the Nimbus look and feel
//         */
//        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
//        /*
//         * If Nimbus (introduced in Java SE 6) is not available, stay with the
//         * default look and feel. For details see
//         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
//         */
//        try {
//            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
//                if ("Nimbus".equals(info.getName())) {
//                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
//                    break;
//                }
//            }
//        } catch (ClassNotFoundException ex) {
//            java.util.logging.Logger.getLogger(PersoonAanmeldDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (InstantiationException ex) {
//            java.util.logging.Logger.getLogger(PersoonAanmeldDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (IllegalAccessException ex) {
//            java.util.logging.Logger.getLogger(PersoonAanmeldDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
//            java.util.logging.Logger.getLogger(PersoonAanmeldDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        }
//        //</editor-fold>
//
//        /*
//         * Create and display the dialog
//         */
//        java.awt.EventQueue.invokeLater(new Runnable() {
//
//            public void run() {
//                PersoonAanmeldDialog dialog = new PersoonAanmeldDialog(new javax.swing.JFrame(), true, new Administratie());
//                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
//
//                    @Override
//                    public void windowClosing(java.awt.event.WindowEvent e) {
//                        System.exit(0);
//                    }
//                });
//                dialog.setVisible(true);
//            }
//        });
//    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnVoegToe;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JComboBox jcbOuder;
    private gui.PersoonPanel persoonPanel2;
    // End of variables declaration//GEN-END:variables
}
